$(function(){
	/*
	 * 初始化 tooltip
	 */
	function getBaseRankLabel(rank,activity){
		var str = "";
		if(rank=="100"){
			str = "岛丁用户";
		}else if(rank=="300"){
			str = "海客";
		}else if(rank=="400"){
			str = "岛邻";
		}else if(rank=="200"&&activity=="1"){
			str = "会员";
		}else if(rank=="200"&&activity=="0"){
			str = "非会员";
		}
		return str;
	}
	
	$('[data-toggle="tooltip"]').tooltip();
	
	var AuditModal = function ($auditModal) {
		this.$auditModal = $auditModal;
		this._initReasonSel();
		this._initConfirmBtn();
	//	this._initValidator();
	};
	AuditModal.prototype = {
		constructor: AuditModal,
		$auditModal: null,
		$auditRow: null,	
		auditId: null,
		auditText: null,
		emptyCheck: false,
		$auditTextarea: null,
//		_initValidator: function() {
//			var self = this;
//			this.$auditTextarea.tooltip({
//				title: '请填写理由，或选择上面的理由',
//				trigger: 'manual'
//			});
////			this.$auditModal.find('form').validate({
////				submitHandler: function() {
////					self.$auditTextarea.tooltip('hide');	
////			    },
////				showErrors: function() {
////					self.$auditTextarea.tooltip('show');	
////				}
////			});
//		},
		_initConfirmBtn: function() {
			var self = this;
			self.$auditModal.on('click','.confirm-btn',function(){
				var advice = self.auditText?self.auditText:self.$auditTextarea.val();
				if(self.emptyCheck) {
					if(self.$auditTextarea.val()=='') {
						greeting({msg:"请填写理由"});
						return false;
					}else{
						doAjax();
					}
				} else {
					doAjax();
				}
				function doAjax() {
					cAjax({
						url:'/cms/eventSign/auditsign',
						data: {
							id: self.$auditRow.data('event-sign-id'),
							advice: advice,
							auditStatus:2
						},
						success: function(obj) {
							self.$auditRow.find('.audit-btn').attr('disabled','');
							self.$auditRow.find('.pass-label').hide();
							self.$auditRow.find('.decline-label').tooltip('destroy').attr('title',advice).tooltip().show();
							self.$auditModal.modal('hide');
							window.location.reload();
						}
					});
				}
			});
		},
		_initReasonSel: function(){
			var $this = this;
			var $ele = this.$auditModal;
			var $textarea = $ele.find('textarea');
			$this.auditText = $.trim($(':radio:checked',$ele).parent().text());
			this.$auditTextarea = $textarea;
			$(':radio',$ele).on('change',function(){
				if($(this).val() == 'user-defined') {
					$textarea.removeAttr('disabled');
					$this.auditText = '';	
					$this.emptyCheck = true;
				} else {
					$textarea.attr('disabled','');
					$textarea.tooltip('hide');	
					$this.emptyCheck = false;
					$this.auditText = $.trim($(this).parent().text());
				}
			});
		},
		open: function ($auditRow) {
			this.$auditRow = $auditRow;
			this.$auditModal.modal('show');
			this.$auditTextarea.val('');
		}	
	};
	var $auditModal = $('#audit-modal');
	$auditModal.modal({
		backdrop: 'static',
		show: false	
	});
	var auditModal = new AuditModal($auditModal);
	$('.btn-danger-event').on('click',function(){
		auditModal.open($(this).parents('tr'));
	});	
	$('.btn-success-event').on('click',function(){
		var eventId = $("input[name='event_id']").val();
		var $tr = $(this).parents('tr');
		cAjax({
			url:"/cms/eventSign/auditValid",
			data:{"eventId":eventId,"uid":$tr.children("td:eq(1)").data('user-id')},
			success:function(obj){
				if(obj.data==0){
					actionConfirm({
						msg: '是否确认审核通过？',
						confirm: function(){
							passAudit($tr);
						}
					});
					
				}else{
					greeting({msg:"错误代码:"+obj.data+""+obj.msg});
				}
			}
		});
	});
	
	function passAudit($tr){
		cAjax({
			url:"/cms/eventSign/auditsign",
			data:{id:$tr.attr("data-event-sign-id"),advice:"通过",auditStatus:1},
			success:function(obj){
				$tr.find('.audit-btn').attr('disabled','');
				$tr.find('.pass-label').hide();
//				$tr.find('.decline-label').tooltip('destroy').attr('title',"通过").tooltip().show();
				$tr.find('.pass-label:eq(1)').tooltip('destroy').attr('title',"通过").tooltip().show();
				$tr.modal('hide');
				window.location.reload();
			}
		});
	}
	$("#event-status").change(function(){
//		var op = $(this).find("optioin:selected").val();
		$("#eventForm").submit();
	});
	//搜索
	$("#signlist-search").on("submit",function(){
		if ($("#search").val()==""){ 
//			greeting({msg:"请输入报名者",title:"提示"}); 
			return true;
			return false;
			
		}else
			return true;
	});
	$("#search-status").change(function() {
		var op = $(this).find("option:selected").val();
		var src = $("#registeredPersonalList-iframe",parent.document).attr("src");
		var _src = src.split("&");
		$("#registeredPersonalList-iframe",parent.document).attr("src",_src[0]+"&status="+op);
	});
	
	//@zhaojp新增支付需求
	$('.datetimepicker').datetimepicker({
		dayViewHeaderFormat: 'YYYY MMMM',
		locale: 'zh-cn',
		format: 'YYYY-MM-DD HH:mm:ss',
		keepInvalid: true
	});	
	$('.datetimepicker').data('DateTimePicker').useCurrent(true);
	
	$('.btn-add-pay').on('click',function(){
		var userName = $(this).closest("tr").find("[data-role='user-name']").html();
		var eventSignId = $(this).closest("tr").data("event-sign-id");
		var amounts = $(this).closest("tr").find('[data-role="pay-amounts"]').html();
		$("#modal-add-pay").find("input[name='user-name']").val(userName);
		$("#modal-add-pay").find("input[name='payment']").val(amounts);
		$("#modal-add-pay").find("input[name='eventSignId']").val(eventSignId);
		$("#modal-add-pay").modal('show');
	});
	$('#btn-sub-pay').on('click',function(){
		var eventSignId = $("#modal-add-pay").find("input[name='eventSignId']").val();
		var payment = $("#modal-add-pay").find("input[name='payment']").val();
		if(payment==""){alerting({msg:"支付金额不能为空！"});return false;}
		var payTime = $("#modal-add-pay").find("input[name='payTime']").val();
		if(payTime==""){alerting({msg:"支付时间不能为空！"});return false;}
		var payType = $("#modal-add-pay").find("input[name='payType']:checked").val();
		var info = $("#modal-add-pay").find("[name='introduce']").val();
		if(info==""){alerting({msg:"支付说明不能为空！"});return false;}
		cAjax({
			url:"/cms/eventSign/pay",
			data:{
				id:eventSignId,
				payment:payment,
				payTime:payTime,
				payType:payType,
				info:info},
			success:function(obj){
				window.location.reload();
			}
		});
	});
	
	
	$('.btn-add-refund').on('click',function(){
		var userName = $(this).closest("tr").find("[data-role='user-name']").html();
		var eventSignId = $(this).closest("tr").data("event-sign-id");
		var amounts = $(this).closest("tr").data('payment');
		$("#modal-add-refund").find("input[name='user-name']").val(userName);
		$("#modal-add-refund").find("input[name='amounts']").val(amounts);
		$("#modal-add-refund").find("input[name='eventSignId']").val(eventSignId);
		$("#modal-add-refund").modal('show');
	});
	$('#btn-sub-refund').on('click',function(){
		var eventSignId = $("#modal-add-refund").find("input[name='eventSignId']").val();
		var amounts = $("#modal-add-refund").find("input[name='amounts']").val();
		if(amounts==""){alerting({msg:"退费金额不能为空！"});return false;}
		var refundTime = $("#modal-add-refund").find("input[name='refundTime']").val();
		if(refundTime==""){alerting({msg:"处理时间不能为空！"});return false;}
		var info = $("#modal-add-refund").find("[name='info']").val();
		if(info==""){alerting({msg:"处理备注不能为空！"});return false;}
		cAjax({
			url:"/cms/eventSign/refund",
			data:{
				id:eventSignId,
				amounts:amounts,
				refundTime:refundTime,
				info:info},
			success:function(obj){
				window.location.reload();
			}
		});
	});
	
	
	$('.btn-add-signer').on('click',function(){
		$("#modal-add-signer").modal('show');
	});
	
	var searchUcUser = function(e){
		var mobile = $(e).val();
		var eventId = $("#modal-add-signer").find("input[name='eventId']").val();
//		var country_code=$("#country_code").val();
//		var reg = /^[0-9]*$/;
		var reg = /^0?1[0|3|4|5|7|8][0-9]\d{8}$/;
		if (!reg.test(mobile)) {
			$(e).focus();
			$(".tip").html("手机号不合法!");
			return false;
		}else{
			$(".tip").html("");
		}
		cAjax({
			url: "/cms/eventSign/searchUcUserByMobile",
			type: "get",
			data: {
				eventId:eventId,
				mobile: mobile
			},
			success: function(obj) {
				console.log(obj["data"]);
				var items = obj["data"];
				var ob_user = items["user"];
				var ob_ding = items["ding"];
				var userPrice =items["userPrice"];
				$("#modal-add-signer").find("input.amounts-span").val(userPrice);
				$("#modal-add-signer").find("span.amounts-span").html(userPrice);
				if(ob_user!=""&&ob_user!=null){//手机号存在
					//询问是否填充下面表单
					actionConfirm({
						msg: "手机号："+mobile+"的用户已存在，是否自动填充表单？",
						confirm: function(){
							$("#modal-add-signer").find("input[name='uid']").val(ob_user.user_id);
							$("#modal-add-signer").find("input[name='needEdit']").val("0");
							$("#modal-add-signer").find("input[name='userName']").val(ob_user.uname).prop({disabled: true});
							$("#modal-add-signer").find("input[name='company']").val(ob_user.com_name).prop({disabled: true});
							$("#modal-add-signer").find("input[name='position']").val(ob_user.position).prop({disabled: true});
							$("#modal-add-signer").find("input[name='userRankLabel']").val(getBaseRankLabel(ob_user.base_rank,ob_user.activity));
							$("#modal-add-signer").find(".service-name").val(ob_ding.uname);
							$("#modal-add-signer").find(".service-mobile").val(ob_ding.mobile);
							if(ob_user.base_rank=="200"&&ob_user.activity=="0"){//未激活的可编辑
								$("#modal-add-signer").find("input[name='needEdit']").val("1");
								$("#modal-add-signer").find("input[name='userName']").prop({disabled: false});
								$("#modal-add-signer").find("input[name='company']").prop({disabled: false});
								$("#modal-add-signer").find("input[name='position']").prop({disabled: false});
							}
							$("#modal-add-signer .hide-area").show();
						},
						cancle:function(){
							$(e).val('');
							$("#modal-add-signer").find("input[name='uid']").val('');
							$("#modal-add-signer").find("input[name='userName']").val('').prop({disabled: false});
							$("#modal-add-signer").find("input[name='company']").val('').prop({disabled: false});
							$("#modal-add-signer").find("input[name='position']").val('').prop({disabled: false});
							$("#modal-add-signer").find("input[name='userRankLabel']").val("非会员");
							$("#modal-add-signer").find(".service-name").val('');
							$("#modal-add-signer").find(".service-mobile").val('');
							$("#modal-add-signer .hide-area").hide();
						}
					});
				}else{//不存在  新增
					$("#modal-add-signer").find("input[name='uid']").val('');
					$("#modal-add-signer").find("input[name='userName']").val('').prop({disabled: false});
					$("#modal-add-signer").find("input[name='company']").val('').prop({disabled: false});
					$("#modal-add-signer").find("input[name='position']").val('').prop({disabled: false});
					$("#modal-add-signer").find("input[name='userRankLabel']").val("非会员");
					$("#modal-add-signer").find(".service-name").val('').parent().parent().hide();
					$("#modal-add-signer").find(".service-mobile").val('').parent().parent().hide();
					$("#modal-add-signer .hide-area").show();
				}
			}
		});
	}
	//搜索用户
	$("#modal-add-signer input[name='mobile']").on("focus",function(){
//		actionConfirm({
//			msg: '确认要修改手机号码',
//			confirm: function(){
//				$("#modal-add-signer .hide-area").hide();
//			}
//		});
		$("#modal-add-signer .hide-area").hide();
	})
//	//搜索用户
	$("#modal-add-signer .searchUcUser").on("click",function(){
		searchUcUser($(this).prev());  
	})
	$("#btn-sub-signer").on("click",function(){
		var formData = {};
		formData["eventId"] = $("#modal-add-signer").find("input[name='eventId']").val();
		formData["needEdit"] = $("#modal-add-signer").find("input[name='needEdit']").val();
		formData["mobile"]  = $("#modal-add-signer").find("input[name='mobile']").val();
		if(formData["mobile"]==""){alerting({msg:"手机号不能为空！"});return false;}
		formData["userName"]  = $("#modal-add-signer").find("input[name='userName']").val();
		formData["company"]  = $("#modal-add-signer").find("input[name='company']").val();
		formData["position"]  = $("#modal-add-signer").find("input[name='position']").val();
		formData["amount"]  = $("#modal-add-signer").find("input[name='amounts']:checked").val();
		formData["waysToPay"]  = $("#modal-add-signer").find("input[name='waysToPay']:checked").val();
		formData["userResource"]  = $("#modal-add-signer").find("#white-sign-source option:selected").val();
		formData["uid"] = $("#modal-add-signer").find("input[name='uid']").val();
		if(formData["uid"]==""){
			delete formData["uid"];
			if(formData["userName"]==""){alerting({msg:"姓名不能为空！"});return false;}
			if(formData["company"]==""){alerting({msg:"公司不能为空！"});return false;}
			if(formData["position"]==""){alerting({msg:"职位不能为空！"});return false;}
		}
		if(formData["userResource"]==""){
			delete formData["userResource"];
		}
		formData["wantedInvoice"]  = $("#modal-add-signer").find("input[name='wantedInvoice']:checked").val();
		if(formData["wantedInvoice"]==""){
			delete formData["wantedInvoice"];
		}
		formData["invoicePhone"]  = $("#modal-add-signer").find("input[name='invoicePhone']").val();
		if(formData["invoicePhone"]==""){
			delete formData["invoicePhone"];
		}
		cAjax({
			url: "/cms/eventSign/addSigner",
			data: formData,
			success: function(obj) {
				window.location.reload();
			}
		});
	})
});
